<template>
<uni-shadow-root class="partials-courseList-index"><block v-if="(!receiveData.androidTemplate||receiveData.androidTemplate=='typeOne')">
  <view class="course_list" v-if="courseList&&courseList.length!=0" :style="'margin-top:'+(limitState!=0?'0rpx':'100rpx')">
      <block v-for="(item,index) in (courseList)" :key="item.id">
          <view class="course_item" :data-link="'course_detail.html?instanceId='+(item.id)" @click="tolinkUrl" v-if="limitState?limitState>index:'true'">
              <view class="course_item_left">
                  <image :src="item.banner" class="course_item_banner" mode="aspectFill"></image>
                  <text class="course_item_tag" v-if="item.chapterSectionType==1">专栏</text>
                  <text class="course_item_tag" v-else-if="item.chapterSectionType==2">系列课</text>
                  <text class="course_item_tag" v-else>课程</text>
              </view>
              <view class="course_item_right">
                  <text class="course_item_name">{{item.courseTitle}}</text>
                  <text class="course_item_describe">{{item.description}}</text>
                  <scroll-view class="tags_content" scroll-x v-if="item.tags">
                    <text v-for="(item,index) in (item.tagArray)" :key="item.index" class="tags_item">{{item}}</text>
                  </scroll-view>
                  <view class="course_item_right_bottom">
                      <text class="course_item_num" v-if="item.sectionCount&&item.sectionCount>1">已更新{{item.sectionCount}}课时</text>
                      <text class="course_item_num" v-else></text>
                      <text class="course_item_price_type" v-if="item.coursePrice<=0">免费</text>
                      <text class="course_item_price_type" v-else>￥{{item.coursePrice}}</text>
                  </view>
              </view>
          </view>
      </block>
  </view>
  <view class="noproduct" v-if="(!courseList||courseList.length==0)" :style="'margin-top:'+(limitState!=0?'0rpx':'100rpx')">
    <image mode="widthFix" src="https://image1.sansancloud.com/xianhua/2019_12/16/15/54/55_950.jpg?x-oss-process=style/preview_120"></image>
    <text>没有相关课程</text>
  </view>
</block>
<block v-if="receiveData.androidTemplate == 'typeTwo'">
  
</block></uni-shadow-root>
</template>

<script>

global['__wxRoute'] = 'partials/courseList/index'
const app = getApp();
Component({
  properties: {
    receiveData: {
      type: Object,
      value: 'default value',
    },
  },
  data: {
    setting: null, 
    courseList: null,
    limitState:0,
    params:null,
    listPage:{
        page: 1,
        pageSize: 20,
        totalSize: 0,
    },
  },
  ready: function () {
    let that = this;
    this.setData({ setting: app.globalData.setting })
    console.log("====courseList-data=====", that.data.receiveData);
    that.initSetting(that.data.receiveData);
  },
  methods: {
    setDataFun:function(data){
        let that=this;
        console.log("setDataFun",data)
        that.setData({params: data})
        that.getData(data)
    },
    /* 获取数据 */
    getData: function (param) {
      var that = this
      let params=Object.assign({},param,{
      })
      console.log("===params==",params)
      var customIndex = app.globalData.AddClientUrl("/wx_find_courses_instances.html", params)
      app.globalData.showToastLoading('loading', true)
      wx.request({
        url: customIndex.url,
        header: app.globalData.header,
        success: function (res) {
          wx.hideLoading()
          console.log(res.data)
          let data=res.data.relateObj.result
          that.data.listPage.pageSize = res.data.relateObj.pageSize
          that.data.listPage.page = res.data.relateObj.curPage
          that.data.listPage.totalSize = res.data.relateObj.totalSize
          let dataArr = that.data.courseList;
          let tagArray=[];
          for (let i = 0; i < data.length; i++) {
            if (data[i].tags && data[i].tags != '') {
              tagArray = data[i].tags.slice(1, -1).split("][")
              data[i].tagArray = tagArray;
            }
          }
          console.log("===data====",data)
          if (param.page == 1) {
            dataArr = []
          }
          if (!data || data.length == 0) {
            that.setData({ courseList: [] })
          } else {
            if (dataArr == null) { dataArr = [] }
            dataArr = dataArr.concat(data)
            that.setData({ courseList: dataArr })
          }
          wx.hideLoading()
        },
        fail: function (res) {
          console.log("fail")
          wx.hideLoading()
          app.globalData.loadFail()
        }
      })
    },
    calling: function (e) {
      console.log('====e===', e)
      let phoneNumber = e.currentTarget.dataset.phonenumber
      wx.makePhoneCall({
        phoneNumber: phoneNumber, //此号码并非真实电话号码，仅用于测试
        success: function () {
          console.log("拨打电话成功！")
        },
        fail: function () {
          console.log("拨打电话失败！")
        }
      })
    },
    toIndex() {
      app.globalData.toIndex()
    },
    tolinkUrl: function (e) {
      let linkUrl = e.currentTarget.dataset.link
      app.globalData.linkEvent(linkUrl)
    },
    initSetting(data) {
      let that=this;
      if (data.jsonData ) {
          that.data.params=Object.assign({},that.data.params,data.jsonData)
          that.getData(that.data.params)
          if(data.jsonData.count){
              that.setData({ limitState: data.jsonData.count })
          }
      }
      console.log("====courseList-limitState=====", that.data.limitState)
      console.log("====courseList-params=====", that.data.params)
    },
    pullDownRefreshFun: function () {
      this.data.params.name = ""
      this.data.listPage.page = 1
      this.data.params.page = 1
      this.getData(this.data.params)

    },
    reachBottomFun: function () {
      var that = this
      if (that.data.listPage.totalSize > that.data.listPage.curPage * that.data.listPage.pageSize) {
        that.data.listPage.page++
        that.data.params.page++
        that.getData(that.data.params);
      }else{
          wx.showToast({
            title: "到底了~",
            image: '/wxcomponents/images/icons/tip.png',
            duration: 1000
          });
      }
    },
  }
})
export default global['__wxComponents']['partials/courseList/index']
</script>
<style platform="mp-weixin">
@import 'type_one/index.css';
</style>